<?php
namespace frontend\widgets;

use frontend\models\Page;
use yii\bootstrap\Widget;

class BreadcrumbsInPage extends Widget
{
    public $currentNode;

    private $node;
    private $allParentsNode;
    
    public function init()
    {
        parent::init();

        $this->allParentsNode = $this->currentNode->getParents()
            ->select('id, alias, title')
            ->asArray()
            ->all();
    }

    public function run()
    {   
        echo '<div class="common-page-header__breadcrumbs">
                <span><a href="/">Главная</a></span>';
                for ($i=1; $i < count($this->allParentsNode); $i++)
                {
                    echo '<span> / </span>';
                    echo '<span><a href="' . $this->generateURL($i) . '">' . $this->allParentsNode[$i]['title'] . '</a></span>';
                }
                echo '<span> / </span>
                <span>' . $this->currentNode->title . '</span>
        </div>';
    }

    
    private function generateURL($i)
    {
        $path = '/';

        for ($j=1; $j <= $i; $j++) 
        { 
            $path .= $this->allParentsNode[$j]['alias'];
        }

        return $path;
    }
    


}